package zx.x.modules.yshapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zx.x.common.utils.R;
import zx.x.modules.app.entity.UserEntity;
import zx.x.modules.app.service.UserService;
import zx.x.modules.yshapp.dao.RaffleAppDao;
import zx.x.modules.yshapp.dao.RaffleSignUpUserAppDao;
import zx.x.modules.yshapp.entity.*;
import zx.x.modules.yshapp.service.*;
import zx.x.modules.yshapp.vo.RaffleVo;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 收货地址
 */
@RestController
@RequestMapping("yshapp/deliveryAddress")
public class DeliveryAddressAppController {
    @Autowired
    private DeliveryAddressService deliveryAddressService;
    //增
    @RequestMapping("/add")
    public R add(DeliveryAddressEntity deliveryAddressEntity){
        Integer count = deliveryAddressService.count();
        if(count>5){
            return R.error("最多只能添加5个收货地址");
        }
        if (deliveryAddressEntity.getDefaultAddress()== 0) {
            DeliveryAddressEntity entity=deliveryAddressService
                    .getOne(new QueryWrapper<DeliveryAddressEntity>()
                            .eq("user_id", deliveryAddressEntity.getUserId())
                            .eq("default_address", 0));
            if(entity!=null){
                entity.setDefaultAddress(1);
                deliveryAddressService.updateById(entity);
            }
        }

        deliveryAddressEntity.setUpdateTime(new Date());
        deliveryAddressService.save(deliveryAddressEntity);
        return R.ok();
    }
    //删
    @RequestMapping("/del")
    public R del(Integer id){
        deliveryAddressService.removeById(id);
        return R.ok();
    }
    // 改
    @RequestMapping("/update")
    public R update(DeliveryAddressEntity deliveryAddressEntity){
        if (deliveryAddressEntity.getDefaultAddress()== 0) {
            DeliveryAddressEntity entity=deliveryAddressService
                    .getOne(new QueryWrapper<DeliveryAddressEntity>()
                            .eq("user_id", deliveryAddressEntity.getUserId())
                            .eq("default_address", 0));
            if(entity!=null){
                entity.setDefaultAddress(1);
                deliveryAddressService.updateById(entity);
            }
        }
        deliveryAddressEntity.setUpdateTime(new Date());
        deliveryAddressService.updateById(deliveryAddressEntity);

        return R.ok();
    }
    //修改默认地址
    @RequestMapping("/updateDefaultAddress")
    public R updateDefaultAddress(DeliveryAddressEntity deliveryAddressEntity){
        DeliveryAddressEntity entity=deliveryAddressService
                .getOne(new QueryWrapper<DeliveryAddressEntity>()
                        .eq("user_id", deliveryAddressEntity.getUserId())
                        .eq("default_address", 0));
        if(entity!=null){
            entity.setDefaultAddress(1);
            deliveryAddressService.updateById(entity);
        }

        deliveryAddressService.updateById(deliveryAddressEntity);
        return R.ok();
    }
    //查单
    @RequestMapping("/detail")
    public R detail(Integer id){
        DeliveryAddressEntity deliveryAddressEntity=deliveryAddressService.getById(id);
        return R.ok().put("deliveryAddress",deliveryAddressEntity);
    }
    //查表
    @RequestMapping("/list")
    public R list(Integer userId){
        List<DeliveryAddressEntity> list=deliveryAddressService
                .list(new QueryWrapper<DeliveryAddressEntity>()
                        .eq("user_id", userId)
                        .orderBy(true,false,"update_time"));
        return R.ok().put("list",list);
    }

}
